<div class="layui-card">
    <form id="app-fromList" lay-filter="app-fromList" class="layui-form model-form" style="width: 60%">
        <input type="hidden" name="id" id="id">
        <div class="layui-card-body">
            <div class="layui-form-item">
                <label class="layui-form-label">店铺名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="sellerName" placeholder="店铺名称"  style="width: 350px;" required lay-verify="sellerName"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图片</label>
                <input type="hidden" name="logo" id="logo">
                <div class="layui-upload-drag" id="test1" style="width: 100px;height: 50px; margin-top: -40px">
                    <label class="upload-label layui-uploader-box req imgLabel"><h1>+</h1></label>
                </div>
                <img src="https://yixun-images.oss-cn-shenzhen.aliyuncs.com/c251cdb1309646489b0e5036a81e7469.png"
                     class="image-path" id="images" style="width:150px;height:120px; margin-top: 0px;"/>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-inline">
                    <input type="text" name="age" id="age" placeholder="年龄"  style="width: 350px;" maxlength="2" required lay-verify="age"
                           class="layui-input" onkeyup="if(this.value.length==1)
                    {this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')} ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">体重</label>
                <div class="layui-input-inline">
                    <input type="text" name="weight" id="weight"  style="width: 350px;" maxlength="3" placeholder="体重(Kg)" required
                           lay-verify="weight"
                           class="layui-input" onkeyup="if(this.value.length==1)
                    {this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')} ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">星座标识</label>
                <div class="layui-input-inline">
                    <input type="text" name="constellation" placeholder="星座标识"  style="width: 350px;" required lay-verify="constellation"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所在地</label>
                <div class="layui-input-inline">
                    <input type="text" name="sellerAddress" placeholder="所在地"  style="width: 350px;" required lay-verify="sellerAddress"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">店铺理念</label>
                <div class="layui-input-inline">
                    <input type="text" name="notice" style="width: 350px;" placeholder="店铺理念" required lay-verify="notice"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label ">店铺描述</label>
                <div class="layui-input-block">
                    <textarea id="demo" name="description"></textarea>
                    <!--<input type="hidden" name="description" id="description"/>-->
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-filter="app-formList-submit" lay-submit>保存</button>
                </div>

            </div>
        </div>

    </form>
</div>
</div>
<script>
    var methods
    layui.use(['tools', 'form', 'util', 'layedit', 'setter', 'admin', 'uploadX', 'upload'], function () {
        var form = layui.form;
        var tools = layui.tools;
        var config = layui.setter;
        var layer = layui.layer;
        var layedit = layui.layedit;
        var uploader = layui.uploadX;
        var upload = layui.upload;
        var indexs;


        //执行方法
        methods = {
            initData() {
                tools.request({
                    url:  '/mall/api/seller/info',
                    method: "get",
                }).done(function (res) {
                    if (res.code == 0) {
                        var data = res.data;
                        if (data) {
                            form.val('app-fromList', data);
                            methods.upload();
                            methods.texts();
                            $("#id").val(data.id);
                            $("#images").attr("src", data.logo + "?x-oss-process=image/resize,m_pad,h_100,w_100");
                            $('#app-form').attr('method', 'POST');
                        }
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                })

            },
            upload() {
                uploader.uploadFile({
                    elem: "#test1",
                    url:  "/cms/file/images",
                    done: function (res, index, upload) {
                        layer.closeAll('loading');
                        if (res.code == 0) {
                            $("#images").attr("src", res.data + "?x-oss-process=image/resize,m_pad,h_100,w_100");
                            $("#logo").val(res.data);
                        }
                    }
                })
            },
            doSubmit: function () {
                form.on('submit(app-formList-submit)', function (data) {
                    //获取富文本的值
                    var content = layedit.getContent(indexs);
                    data.field.description = content;
                    tools.request({
                        url: '/mall/seller/update',
                        method: "post",
                        params: data.field
                    }).done(function (res) {
                        if (res.code == 0) {
                            layer.msg("保存成功", {icon: 1,time: 2000});
                            window.location.reload()
                        } else {
                            layer.msg("操作失败", {icon: 5});
                        }
                    })
                    return false;
                })
            },
            texts() {
                let tk=config.getToken();
                let header;
                if(tk){
                    header = {Authorization: `Bearer `+tk};
                }
                //建立富文本编辑器
                indexs = layedit.build('demo', {
                    height: 400,//设置编辑器高度
                    uploadImage: {
                        url: 'http://360huiyun.com/gw/cms/file/product',
                        headers: header
                    }
                });
            }
        }
        methods.initData();
        methods.doSubmit();

    });
</script>
